﻿using System;
using COMAdmin;
using System.Collections.Specialized;

namespace Sinacor.OMM.Util
{
    public static class ComPlus
    {
        #region [ Objects ]

        static COMAdminCatalog objAdmin;
        static COMAdminCatalogCollection objCollection;

        #endregion

        #region [ Internal Methods ]

        internal static COMAdminCatalogCollection GetCollection()
        {
            objAdmin = new COMAdmin.COMAdminCatalog();
            objCollection = (COMAdmin.COMAdminCatalogCollection)objAdmin.GetCollection("Applications");

            return objCollection;
        }

        #endregion

        #region [ Public Methods ]

        public static NameValueCollection GetCOMApplications()
        {
            NameValueCollection collection = new NameValueCollection();

            try
            {
                objCollection = GetCollection();
                objCollection.Populate();

                foreach (COMAdmin.COMAdminCatalogObject objAppNames in objCollection)
                {
                    COMAdmin.ICatalogCollection objComponents = (COMAdmin.ICatalogCollection)objCollection.GetCollection("Components", objAppNames.Key);

                    objComponents.Populate();

                    foreach (COMAdmin.COMAdminCatalogObject Components in objComponents)
                    {
                        collection.Add(Components.Name.ToString(), objAppNames.Name.ToString());
                    }
                }
            }
            catch
            {
                collection = null;
            }

            return collection;
        }

        public static bool DeleteCOMApplication(string appName)
        {
            bool COMApplicationDeleted = false;
            try
            {
                long l_Count = 0;
                ICatalogCollection pCollection = GetCollection();
                ICatalogObject pCatalog;
                pCollection.Populate();
                l_Count = pCollection.Count;

                if (l_Count == 0)
                    return true;

                for (int i = 0; i < l_Count; i++)
                {
                    pCatalog = (ICatalogObject)pCollection.get_Item(i);
                    if (appName == (string)pCatalog.get_Value("Name"))
                    {
                        pCollection.Remove(i);
                        pCollection.SaveChanges();
                        COMApplicationDeleted = true;
                        break;
                    }
                }
            }
            catch
            {
                COMApplicationDeleted = false;
            }

            return COMApplicationDeleted;
        }

        public static bool ApplicationExists(string name)
        {
            bool applicationExists = false;
            long l_Count = 0;
            ICatalogCollection pCollection = GetCollection();
            ICatalogObject pCatalog;

            pCollection.Populate();

            l_Count = pCollection.Count;

            if (l_Count == 0)
                return applicationExists;

            for (int i = 0; i < l_Count; i++)
            {
                pCatalog = (ICatalogObject)pCollection.get_Item(i);
                if (name.ToUpper() == (string)pCatalog.get_Value("Name").ToString().ToUpper())
                {
                    applicationExists = true;
                    break;
                }
            }

            return applicationExists;
        }

        public static bool StartCOMApplication(string applicationName)
        {
            bool Startup = false;
            try
            {
                objCollection = GetCollection();    // Method returns
                objAdmin.StartApplication(applicationName);
                Startup = true;
            }
            catch
            {
                Startup = false;
            }
            return Startup;
        }

        public static bool InstallCOMComponent(string appName, string appDescription, string componentPath, out string errorMessage)
        {
            bool COMComponentInstalled = false;
            errorMessage = String.Empty;

            if (CreateCOMApplication(appName, appDescription))
            {
                try
                {
                    objAdmin.InstallComponent(appName, componentPath, "", "");
                    COMComponentInstalled = true;
                }
                catch (Exception ex)
                {
                    errorMessage = ex.Message;
                    COMComponentInstalled = false;
                }

            }

            return COMComponentInstalled;

        }

        public static bool ShutDownCOMApplication(string applicationName)
        {
            bool ShuttingDown = false;
            try
            {
                objCollection = GetCollection();
                objAdmin.ShutdownApplication(applicationName);
                ShuttingDown = true;
            }
            catch
            {
                ShuttingDown = false;
            }
            return ShuttingDown;
        }

        #endregion

        #region [ Private Methods ]

        private static bool CreateCOMApplication(string name, string description)
        {
            bool COMApplicationCreated = false;

            try
            {
                if (ApplicationExists(name))
                    DeleteCOMApplication(name);


                objCollection = GetCollection();
                COMAdminCatalogObject objObject = (COMAdmin.COMAdminCatalogObject)objCollection.Add();
                objObject.set_Value("Name", name);
                objObject.set_Value("Description", description);
                objObject.set_Value("Activation", COMAdmin.COMAdminActivationOptions.COMAdminActivationInproc);

                objCollection.SaveChanges();
                COMApplicationCreated = true;
            }
            catch
            {
                COMApplicationCreated = false;
            }

            return COMApplicationCreated;
        }

        #endregion
    }
}
